package repositories;

import java.util.Collection;
import java.util.Date;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import domain.Auction;

@Repository
public interface AuctionRepository extends JpaRepository<Auction, Integer> {
	
	@Query("select a from Auction a where a.startMoment<=?1 and a.finishMoment>?1")
	Collection<Auction> findActiveAuctions(Date date);
	
	@Query("select a from Auction a where a.bids.size=(select max(aa.bids.size)from Auction aa )")
	Collection<Auction> findByMoreBids();
	
	@Query("select a from Auction a where a.finishMoment<?1")
	Collection<Auction> findFinishedAuctions(Date date);
	
	@Query("select a from Auction a where a.customer.id=?1")
	Collection<Auction> findAuctionsByCustomerId(int customerId);
	
	@Query("select a from Auction a where a.finishMoment<?1 and a.customer.id=?2")
	Collection<Auction> findFinishedAuctionsByCustomer(Date date, int customerId);
	
	@Query("select a from Auction a where a.startMoment<=?1 and a.finishMoment>?1 and a.customer.id!=?2")
	Collection<Auction> findActiveAuctionsByOther(Date date, int customerId);
	
	@Query("select a from Auction a where a.startMoment<=?1 and a.finishMoment>?1 and a.painting.id=?2")
	Collection<Auction> findActiveAuctionsByPainting(Date date, int paintingId);
	
	@Query("select a from Auction a where a.finishMoment>?1 and a.painting.id=?2")
	Auction findPendingAuctionByPaintingId(Date date, int paintingId);
}
